package com.jswj.screenshot.controller;

import java.util.List;

import com.jswj.media.task.ScreenshotTask;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.jswj.screenshot.domain.Screenshot;
import com.jswj.screenshot.service.IScreenshotService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 截图Controller
 * 
 * @author whh
 * @date 2021-07-07
 */
@Controller
@RequestMapping("/screenshot/screenshot")
public class ScreenshotController extends BaseController
{
    private String prefix = "screenshot/screenshot";

    @Autowired
    private IScreenshotService screenshotService;

    @RequiresPermissions("screenshot:screenshot:view")
    @GetMapping()
    public String screenshot()
    {
        return prefix + "/screenshot";
    }

    /**
     * 查询截图列表
     */
    @RequiresPermissions("screenshot:screenshot:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Screenshot screenshot, Model model)
    {
        startPage();
        List<Screenshot> list = screenshotService.selectScreenshotList(screenshot);
        model.addAttribute("imgPath", ScreenshotTask.IMG_PATH);
        return getDataTable(list);
    }

    /**
     * 导出截图列表
     */
    @RequiresPermissions("screenshot:screenshot:export")
    @Log(title = "截图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Screenshot screenshot)
    {
        List<Screenshot> list = screenshotService.selectScreenshotList(screenshot);
        ExcelUtil<Screenshot> util = new ExcelUtil<Screenshot>(Screenshot.class);
        return util.exportExcel(list, "截图数据");
    }

    /**
     * 新增截图
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存截图
     */
    @RequiresPermissions("screenshot:screenshot:add")
    @Log(title = "截图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Screenshot screenshot)
    {
        return toAjax(screenshotService.insertScreenshot(screenshot));
    }

    /**
     * 修改截图
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Screenshot screenshot = screenshotService.selectScreenshotById(id);
        mmap.put("screenshot", screenshot);
        return prefix + "/edit";
    }

    /**
     * 修改保存截图
     */
    @RequiresPermissions("screenshot:screenshot:edit")
    @Log(title = "截图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Screenshot screenshot)
    {
        return toAjax(screenshotService.updateScreenshot(screenshot));
    }

    /**
     * 删除截图
     */
    @RequiresPermissions("screenshot:screenshot:remove")
    @Log(title = "截图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(screenshotService.deleteScreenshotByIds(ids));
    }
}
